;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Purchase.PotentialSupplier", [])
        .controller("PotentialSupplierCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "UserContextService", "$q", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, UserContextService, $q) {
            $scope.activeFlag = [true, false];
            $scope.markInfo = {
                allChecked: false
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.condition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.dataList = [];

            $scope.tbConf = {
                fixedWidth: 200,
                scrollWidth: 2900,
                tableCol: [
                    {
                        name: "申请编号",
                        width: 180,
                        dataField: "applyCode",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "供方名称",
                        width: 150,
                        dataField: "supplierName",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "申请时间",
                        width: 120,
                        dataField: "applyTimeShow",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "大品类",
                        width: 150,
                        dataField: "majorCategory",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "小品类",
                        width: 150,
                        dataField: "minorCategory",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "适用机型",
                        width: 150,
                        dataField: "machineModel",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "联系人",
                        width: 150,
                        dataField: "contactName",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "联系方式",
                        width: 120,
                        dataField: "contactTel",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "企业性质",
                        width: 120,
                        dataField: "companyType",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "详细地址",
                        width: 150,
                        dataField: "companyAddress",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "成立时间",
                        width: 120,
                        dataField: "companyFoundingTimeShow",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "注册资金",
                        width: 120,
                        dataField: "companyRegisterMoney",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "当年年产值",
                        width: 120,
                        dataField: "annualOutputMoney",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "员工总数量",
                        width: 120,
                        dataField: "employeeNumber",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "研发人员数量",
                        width: 120,
                        dataField: "researcherNumber",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "体系文件",
                        width: 150,
                        dataField: "systemDocument",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "厂房持有",
                        width: 120,
                        dataField: "workshopType",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "主要客户情况",
                        width: 150,
                        dataField: "mainCustomerInfo",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "主要销售渠道",
                        width: 150,
                        dataField: "mainSaleChannel",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "备注",
                        width: 150,
                        dataField: "remark",
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "审核状态",
                        dataField: "auditStatusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 160,
                        isShow: true,
                        isFixed: true,
                    }
                ]
            };

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    scrollLeft: {left: $scope.tbConf.fixedWidth + "px"},
                    scrollLeftWidth: {width: $scope.tbConf.fixedWidth + "px"}
                };
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.selectAll = function () {
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    $scope.dataList[i].isChecked = $scope.markInfo.allChecked;
                }
            };

            function checkAll() {
                var selectAll = false;
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!$scope.dataList[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectSingle = function (item) {
                if (!item.isChecked) {
                    $scope.markInfo.allChecked = false;
                } else {
                    if (!checkAll()) {
                        $scope.markInfo.allChecked = false;
                    } else {
                        $scope.markInfo.allChecked = true;
                    }
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            function getDataList(curPage) {
                $scope.markInfo.allChecked = false;
                var service = "device";
                var reqUrl = "/wjSupplierApply/list";
                
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                var param = {
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage,
                    filters: $scope.condition
                };
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.applyTimeShow = $filter("YbDate")(item.applyTime);
                        item.companyFoundingTimeShow = $filter("YbDate")(item.companyFoundingTime);
                        item.auditStatusShow = $filter("ProgressStatus")(item.auditStatus, "supplierState");
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.auditData = function (id) {
                ybWidget.AuditMsg({
                    noAuditMsg: true,
                    passCb: function(val) {
                        return handleAudit (id, true)
                    },
                    refuseCb: function(val) {
                        return handleAudit (id, false)
                    }
                })
            };

            function handleAudit(id, pass) {
                return ybHttp.Get("device", "/wjSupplierApply/audit", { id, pass }).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                });
            }

            $scope.delData = function (id) {
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Get("device", "/wjSupplierApply/remove", {id}).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                }, function () {

                })
            };

            $scope.genEditUrl = function (id) {
                var service = "device";
                var reqUrl = "/wjSupplierApply/genAccessUrl";
                ybHttp.Get(service, reqUrl, {id: id}).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowMessage(resp.value, MESG_TYPE.Success, { isHtml: true, isQrCode: true });
                });
            };

            $scope.recordUrl = window.AppData.config.baseUrl.PotentialSupplierPage;


            (function () {
                getDataList();
                $scope.setFixedWidth();
            })();
        }])
    ;
})(window, window.angular);
